import { Flex } from 'antd';
import LineSegment from '@/pages/iot/components/LineSegment.tsx';
import { createStyles } from 'antd-style';
import { useQuery } from '@tanstack/react-query';
import { useCatch } from '@/hooks';
import IotServices from '@/api/services/iotServices.ts';
import { FC } from 'react';

const useStyle = createStyles(({ css }) => {
  const baseLine = css`
    border-radius: 2px;
    overflow: hidden;
    height: 4px;
    margin: 4px 0;
  `;

  return {
    dataSummaryWrapper: css`
      color: var(--ycx-font-color-l1);
      padding: 8px 0;

      & .total {
        margin-bottom: 24px;
      }

      & .line-segment-wrapper {
        margin-bottom: 40px;
      }

      & .today-alarm {
        margin: 24px 0;
      }
    `,
    red: css`
      ${baseLine};
      background-color: var(--ycx-color-red);
    `,
    yellow: css`
      ${baseLine};
      background-color: var(--ycx-color-yellow);
    `,
    title: css`
      font-size: 12px;
      font-weight: 400;
      line-height: 20px; /* 166.667% */
      color: var(--ycx-font-color-l3);
    `,
    hugeNum: css`
      //font-family: D-DIN;
      font-size: 64px;
      font-weight: 700;
      line-height: 82px; /* 128.125% */
      margin-top: 8px;
    `,
    lineSegmentNum: css`
      font-size: 24px;
      font-weight: 700;
      line-height: 32px; /* 133.333% */
    `,
    alarmNum: css`
      font-size: 32px;
      font-style: normal;
      font-weight: 700;
      line-height: 40px;
    `,
  };
});

type Props = {
  carNo?: string;
};

const DataSummary: FC<Props> = ({ carNo }) => {
  const { styles } = useStyle();
  const catchFn = useCatch();

  const { data: deviceCount } = useQuery({
    queryKey: ['deviceCount', carNo],
    queryFn: () => catchFn(() => IotServices.getDeviceCount({ carNo })),
    select: ({ data }) => data,
  });

  const { data: alarmCount } = useQuery({
    queryKey: ['alarmCount', carNo],
    queryFn: () => catchFn(() => IotServices.getAlarmCount({ carNo })),
    select: ({ data }) => data,
  });

  return (
    <div className={styles.dataSummaryWrapper}>
      <div className="total">
        <div className={styles.title}>设备数量（台）：</div>
        <div className={styles.hugeNum}>{deviceCount?.deviceTotal}</div>
      </div>

      <div className="line-segment-wrapper">
        <Flex justify="space-between">
          <div>
            <span className={styles.lineSegmentNum}>{deviceCount?.onlineDeviceCount}</span>台
          </div>
          <div>
            <span className={styles.lineSegmentNum}>{deviceCount?.offlineDeviceCount}</span>台
          </div>
        </Flex>
        <LineSegment
          width={`${(deviceCount?.onlineDeviceCount ?? 0) / (deviceCount?.deviceTotal ?? 0)}%`}
        />
        <Flex justify="space-between">
          <div className={styles.title}>在线设备</div>
          <div className={styles.title}>离线设备</div>
        </Flex>
      </div>

      <div>
        <div className={styles.alarmNum}>{alarmCount?.alarmTotal}</div>
        <div className={styles.yellow} />
        <div className={styles.title}>历史报警总数</div>
      </div>

      <div className="today-alarm">
        <div className={styles.alarmNum}>{alarmCount?.todayAlarmCount}</div>
        <div className={styles.red} />
        <div className={styles.title}>今日报警次数</div>
      </div>

      <div>
        <div className={styles.alarmNum}>{alarmCount?.monthAlarmCount}</div>
        <div className={styles.yellow} />
        <div className={styles.title}>本月报警次数</div>
      </div>
    </div>
  );
};

export default DataSummary;
